import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chihb
 * @create-time 2024/7/9 10:55
 * @description
 */// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();
//        test5();
//        test6();
//        test7();
//        test8();
//        test9();
//        test10();
//        test11();
//        test12();
//        test13();
//        test14();
        test15();
    }



    /*
    * 单列集合创建流
    * 输出年龄小于18岁的作家
    * */
    private static void test1(){
        List<Author> author=Author.getAuthors();
        author.stream()
                .distinct()
                .filter(author1 -> author1.getAge()<18)
                .forEach(System.out::println);
    }

    /*
     * 双列集合创建流
     * 打印map里的数据
     * */
    private static void test2(){
        Map<String,Integer> map=new HashMap<>();
        map.put("蜡笔小新",1);
        map.put("皮卡丘",2);
        map.put("孙悟空",3);
        map.entrySet().stream().forEach(e-> System.out.println(e.getKey()+"===="+e.getValue()));
    }

    /**
     * 打印所有名字长度大于1的作家的姓名
     */
    private static void test3(){
        List<Author> author=Author.getAuthors();
        author.stream()
                .distinct()
                .filter(author1 -> author1.getName().length()>1)
                .forEach(s-> System.out.println(s.getName()));
    }
    /**
     * 打印所有名字长度大于1的作家的姓名
     */
    private static void test4(){
        List<Author> author=Author.getAuthors();
        author.stream()
                .distinct()
                .map(Author::getName)
                .forEach(System.out::println);
    }

    /*
    * 对元素中的年龄字段，进行降序排序，不允许有重复字段
    * */
    private static void test5() {
        List<Author> authors = Author.getAuthors();
        authors.stream()
                .distinct()
                .sorted(((o1, o2) -> o2.getAge()- o1.getAge()))
                .forEach(System.out::println);
    }

    /*
     * 打印所有书籍的名字，要求对重复的元素进行去重
     * */
    private static void test6() {
        //打印所有书籍的名字，要求对重复的元素进行去重
        List<Author> authors = Author.getAuthors();
        authors.stream()
                .flatMap(a->a.getBooks().stream())
                .distinct()
                .forEach(s-> System.out.println(s.getName()));
    }

    private static void test7() {
        List<Author> authors = Author.getAuthors();
        authors.stream()
                .flatMap(a->a.getBooks().stream())
                .distinct()
                .flatMap(book-> Arrays.stream(book.getCategory().split(",")))
                .distinct()
                .forEach(System.out::println);
    }

    private static void test8() {
        List<Author> authors = Author.getAuthors();
        long count = authors.stream()
                .flatMap(a -> a.getBooks().stream())
                .distinct()
                .count();
        System.out.println(count);

    }
    private static void test9() {
        List<Author> authors = Author.getAuthors();
        Optional<Integer> max = authors.stream()
                .flatMap(a -> a.getBooks().stream())
                .distinct()
                .map(Book::getScore)
                .max((o1, o2) -> o1 - o2);
        System.out.println(max.get());
        Optional<Integer> min = authors.stream()
                .flatMap(a -> a.getBooks().stream())
                .distinct()
                .map(Book::getScore)
                .min((o1, o2) -> o1 - o2);
        System.out.println(min.get());
    }
    private static void test10() {
        List<Author> authors = Author.getAuthors();
        Map<String, List<Book>> map = authors.stream()
                .distinct()
                .collect(Collectors.toMap(Author::getName, Author::getBooks));
        System.out.println(map);
    }
    //可以判断流中的元素是否有一个符合匹配条件，如果有一个符合结果位true，否则一个都不符合结果为false
    private static void test11() {
        List<Author> authors = Author.getAuthors();
        boolean flag = authors.stream()
                .anyMatch(s -> s.getAge() > 18);
        System.out.println(flag);
    }
    //可以判断流中的元素是否都符合匹配条件，如果不符合结果位true，否则结果为false
    private static void test12() {
        List<Author> authors = Author.getAuthors();
        boolean flag = authors.stream()
                .allMatch(s -> s.getAge() > 18);
        System.out.println(flag);
    }
    //可以判断流中的元素是否都不符合匹配条件，如果都不符合结果位true，否则结果为false
    private static void test13() {
        List<Author> authors = Author.getAuthors();
        boolean flag = authors.stream()
                .noneMatch(s -> s.getAge() > 18);
        System.out.println(flag);
    }

    private static void test14() {
        List<Author> authors = Author.getAuthors();
        Optional<Author> first = authors.stream()
                .filter(s -> s.getAge() > 18)
                .findFirst();
        first.ifPresent(author -> System.out.println(author.getName()));
    }

    private static void test15() {
        List<Author> authors = Author.getAuthors();
        Optional<Author> any = authors.stream()
                .filter(s -> s.getAge() < 18)
                .findAny();
        any.ifPresent(author -> System.out.println(author.getName()));
    }
}